package com.estate.charge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.estate.bean.ChargeDetail;
import com.estate.charge.feign.QuartersFeign;
import com.estate.charge.service.ChargeDetailService;
import com.estate.mapper.ChargeDetailMapper;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.QueryInterceptor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ChargeDetailServiceImpl extends ServiceImpl<ChargeDetailMapper, ChargeDetail> implements ChargeDetailService {

    @Resource
    private QuartersFeign quartersFeign;
    @Override
    public JsonResult listByPage(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<ChargeDetail> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        List<ChargeDetail> list=list(wrapper);
        list.forEach(e->{
            e.setQuarters(quartersFeign.find(e.getCommunityId()));
        });
        PageInfo<ChargeDetail> pageInfo = new PageInfo<>(list);
        return ResultTool.success(pageInfo);
    }
    @Override
    public JsonResult saveChargeDetail(ChargeDetail chargeDetail) {
        chargeDetail.setStatus(0);
//        chargeDetail.setIsDelete(0);
        log.info("新增chargDstail数据:{}",chargeDetail);
        boolean b= save(chargeDetail);
        if (!b){
            return ResultTool.fail(501,"新增失败");
        }
        return ResultTool.success("新增成功");
    }
    @Override
    public JsonResult updateChargeDetail(ChargeDetail chargeDetail) {
        log.info("修改chargDstail数据:{}",chargeDetail);
        boolean b= updateById(chargeDetail);
        if (!b){
            return ResultTool.fail(503,"修改失败");
        }
        return ResultTool.success("修改成功");
    }
    @Override
    public JsonResult deleteChargeDetail(String chargeDetailId) {
        boolean b= removeById(chargeDetailId);
        if (!b){
            return ResultTool.fail(502,"删除失败");
        }
        return ResultTool.success("删除成功");
    }
}
